#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string>
#include "log.h"
#include "haskelltool.h"

namespace HaskellTool {
//****************************************namespace HaskellTool************************************************************
std::vector<std::string>
words(std::string str)
{
    std::vector<std::string>  strList;
    const char *str1 = str.c_str();
    int len = str.length();
    int startIndex;
    int endIndex;
    int i = 0;


    while (1)
    {
        while (isspace(str1[i]))
        {
            i++;
            if (i >= len)
            {
               // logInfo("words: >=len 1");
                goto label1;
            }

        }
        startIndex = i;
        while (!isspace(str1[i]))
        {
            i++;
            if (i >= len)
            {
               // logInfo("words: >=len 2");
                break;
            }
        }
        endIndex = i;
        std::string strItem = std::string(str1 + startIndex, endIndex - startIndex);
        strList.push_back(strItem);
      //  logInfo("strItem=[%s]", strItem.c_str());
        if(i >= len)
        {
            break;
        }
    }

label1:
     //logInfo("i >= len, size=%d", strList.size());
     return strList;
}



std::vector<std::string>
foldl(fun1T fun,   std::vector<std::string>& resultList, std::vector<std::string>& list)
{

    for (std::string &item : list)
    {
        fun(resultList, item);
    }
    return resultList;
}

std::vector<std::string>
foldl(fun1T fun,   std::vector<std::string>& resultList, std::vector<std::string>&& list)
{

    for (std::string &item : list)
    {
        fun(resultList, item);
    }
    return resultList;
}

std::vector<std::string>
foldl2(  std::function<void (std::vector<std::string> &, std::string &)> fun,
     std::vector<std::string> &resultList, std::vector<std::string> &list)
{
    for (std::string &item : list)
    {
        fun(resultList, item);
    }
    return resultList;
}

std::vector<std::string>
foldl2(  std::function<void (std::vector<std::string> &, std::string &)> fun,
     std::vector<std::string> &resultList, std::vector<std::string> &&list)
{
    for (std::string &item : list)
    {
        fun(resultList, item);
    }
    return resultList;
}


std::string
head(std::vector<std::string>&& list)
{
    return list[0];
}

std::string
head(std::vector<std::string>& list)
{
    return list[0];
}


//================================namespace HaskellTool===========================================
}
